package com.example.springbootlearn.netty;


import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.compress.ZipWriter;
import com.example.springbootlearn.netty.dto.BizMsg;
import com.example.springbootlearn.netty.okhttp.TxAsrWebsocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@RequestMapping("netty")
@RestController
public class NettyController {
    @Autowired
    TxAsrWebsocket txAsrWebsocket;


    // 示例方法：模拟查询数据
    private List<String> queryData() {
        return ListUtil.of("Data 1", "Data 2", "Data 3");
    }

    // 1. 原生方法 (推荐)
    @GetMapping("/exportDataOrigin")
    public void exportDataOrigin(HttpServletResponse response) throws IOException {
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment;filename=data.zip");

        // 获取响应输出流
        try (ServletOutputStream servletOutputStream = response.getOutputStream();
             ZipOutputStream zipOutputStream = new ZipOutputStream(servletOutputStream)) {

            // 模拟查询结果
            List<String> data = queryData();

            // 将每个数据写入单独的TXT文件，并添加到ZIP中
            for (int i = 0; i < data.size(); i++) {
                String fileName = "data" + (i + 1) + ".txt";
                zipOutputStream.putNextEntry(new ZipEntry(fileName));

                // 写入TXT文件内容
                String content = data.get(i);
                zipOutputStream.write(content.getBytes());
                zipOutputStream.closeEntry();
            }
        }
    }

    // 2. 工具类
    @GetMapping("/exportData")
    public void exportData(HttpServletResponse response) throws IOException {
        // 设置响应头
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment;filename=data.zip");

        // 创建一个内存中的输出流
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try (ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream)) {
            ZipWriter zipWriter = new ZipWriter(zipOutputStream);

            // 模拟查询结果
            List<String> data = queryData();

            // 将每个数据写入单独的TXT文件，并添加到ZIP中
            for (int i = 0; i < data.size(); i++) {
                String fileName = "data" + (i + 1) + ".txt";
                InputStream inputStream = new ByteArrayInputStream(data.get(i).getBytes());
                zipWriter.add(fileName, inputStream);
            }
        }

        // 将ZIP流写入响应输出流
        try (ServletOutputStream servletOutputStream = response.getOutputStream()) {
            servletOutputStream.write(byteArrayOutputStream.toByteArray());
        }
    }

    // 3. 工具类 (简化版)
    @GetMapping("/simplifyExportData")
    public void simplifyExportData(HttpServletResponse response) throws IOException {
        // 设置响应头
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment;filename=data.zip");
        // 创建一个内存中的输出流
        try (ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream())) {
            ZipWriter zipWriter = new ZipWriter(zipOutputStream);
            // 模拟查询结果
            List<String> data = queryData();

            // 将每个数据写入单独的TXT文件，并添加到ZIP中
            for (int i = 0; i < data.size(); i++) {
                String fileName = "data" + (i + 1) + ".txt";
                InputStream inputStream = new ByteArrayInputStream(data.get(i).getBytes());
                zipWriter.add(fileName, inputStream);
            }
        }
    }


    @RequestMapping("msgInfo/unreadCount")
    public Long getUnreadCount() {
        int i = 1 / 0;
        return 3L;
    }


    // mock send 1000 msg to netty server
    @RequestMapping("sendMsg")
    public String sendMsg() {

        String key = "1000";
        BizMsg bizMsg = new BizMsg();
        bizMsg.setCode("invite");
        bizMsg.setMsg("A");
        St.send(key, bizMsg);
        for (int i = 1; i <= 1000; i++) {
            BizMsg innerbizMsg = new BizMsg();
            innerbizMsg.setCode("invite");
//            innerbizMsg.setMessageId(String.valueOf(i));
            innerbizMsg.setMsg("A_" + i);
            St.send(key, innerbizMsg);
        }
        return "sendMsg success";
    }

    @RequestMapping("sendMsgToA")
    public String sendMsgToA(String key) {

        BizMsg bizMsg = new BizMsg();
        bizMsg.setCode("test");
        bizMsg.setMsg("测试发送数据");
        St.send(key, bizMsg);
        return "sendMsg success";
    }


    @RequestMapping("getConnect")
    public Object getConnect() {
        return St.getMap();
    }

    @RequestMapping("hello")
    public String hello() {
        return "Hello Netty!";
    }

//    mock websocketconnect

    @RequestMapping("websocketconnect")
    public String websocketconnect(String messageId) {
        txAsrWebsocket.initAsrWsClient(messageId);
        return "websocketconnect success";
    }

    @RequestMapping("destroyAll")
    public String destroyAll() {
        txAsrWebsocket.destroyAll();
        return "websocket destroyAll success";
    }
}
